package com.ray.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import com.ray.basic.BASIC;
import com.ray.server.RW_myQ;


public class Server_in  implements Runnable{
	private InputStream in=null;
	private OutputStream out=null;;
	private Socket clntSock=null;
	private int clientid;
	private boolean accept=false;
	private String username= null;
	
 	public Server_in(Socket clntSock, int clientid){
 		this.clntSock=clntSock;
		this.clientid=clientid;
	}
 	
 	public void set_username(String username){
 		this.username=username;
 	}
 	public String get_username(){
 		return username;
 	}
 	public  OutputStream get_out(){
 		return out;
 	}
 	public Socket get_clntSock(){
 		return clntSock;
 	}
 	public boolean get_accept(){
 		return accept;
 	}
 	public void set_accept(boolean status){
 		accept=status;
 	}
	public void run(){
		try {
			in = clntSock.getInputStream();
			out = clntSock.getOutputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		boolean status=true;
		while (status){
			byte [] receiveBuf=new byte[BASIC.get_BUFSIZE()];
			int length=0;
			try {
				length = in.read(receiveBuf);
				String receive= new String(receiveBuf,0,length);
				receive=BASIC.unpack(receive);//unpack package
				System.out.println("Server_in::"+clientid+":"+receive);
				status=new RW_myQ().put(clientid, receive);
			} catch (IOException e) {
				e.printStackTrace();
				break;
			}
		}
	}
}
